﻿using System;
using System.Runtime.InteropServices;
using System.Text;
using GameServer.Virtual_Objects.Room;

namespace GameServer
{
    class Packet
    {
        [DllImport("winmm.dll")]
        public static extern long timeGetTime();

        private int PacketID;
        private string[] blocks = new string[0];
        private bool Client = false;

        public byte[] getBytes()
        {
            try
            {
                string sPacket = String.Empty;

                sPacket =
                    Convert.ToString(timeGetTime()) +
                    Convert.ToChar(0x20) +
                    Convert.ToString(PacketID) +
                    Convert.ToChar(0x20);

                for (int i = 0; i < blocks.Length; i++)
                {
                    sPacket += blocks[i].Replace(Convert.ToChar(0x20), Convert.ToChar(0x1D)) + Convert.ToChar(0x20);
                }


                sPacket = Crypt2(sPacket + Convert.ToChar(0x20) + Convert.ToChar(0x0A));


                return Encoding.Default.GetBytes(sPacket);
            }
            catch
            {
                return null;
            }
        }

        protected void newPacket(int PacketID)
        {
            this.PacketID = PacketID;
        }

        protected void setClient(bool C)
        {
            this.Client = C;
        }

        protected void addBlock(object Block)
        {
            try
            {
                Array.Resize(ref blocks, blocks.Length + 1);
                blocks[blocks.Length - 1] = Convert.ToString(Block);
            }
            catch { }
        }

        public string Crypt2(string sPacket)
        {
            byte[] tTemp = Encoding.Default.GetBytes(sPacket);

            for (int i = 0; i < tTemp.Length; i++)
            {
                tTemp[i] = Convert.ToByte(tTemp[i] ^ 0x11); //45
            }
            return Encoding.Default.GetString(tTemp);
        }

        public void addRoomInfo(virtualRoom Room)
        {
            try
            {
                addBlock(Room.ID);
                addBlock(1);
                addBlock(Room.RoomStatus);
                addBlock(Room.RoomMasterSlot);
                addBlock(Room.Name);
                addBlock(Room.EnablePassword);
                addBlock(Room.MaxPlayers);
                addBlock(Room.PlayerCount);
                addBlock(Room.MapID);
                addBlock(Room.Rounds);
                addBlock(Room.Kills);
                addBlock(Room.TimeLimit);
                addBlock(Room.Mode);
                addBlock(4);
                addBlock(Room.isJoinAble() ? 1 : 0); // 0 = unjoinable(grey room) // Once We are almost over
                addBlock(3);
                addBlock(Room.SuperMaster ? 1 : 0); // 1 = Room has Supermaster
                addBlock(Room.RoomType);
                addBlock(Room.LevelLimit);
                addBlock(Room.PremiumOnly);
                addBlock(Room.VoteKick);
                addBlock(0);
                addBlock(Room.Ping);
                addBlock(Room.Ping);
                addBlock(-1);
            }
            catch { }
        }
    }
}